package com.study.jyg.rxjava2study.utils;

import android.widget.AdapterView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.widget.UniversalItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class AdapterInit {

    /**
     * 初始化adapter
     *
     * @return 实例化适配器
     */

    public static <T> BaseQuickAdapter<T, BaseViewHolder> initRecyclerView(RecyclerView recyclerView, int layout, RecyclerView.LayoutManager manager, AdapterInitListener<T> listener) {
        return initRecyclerView(recyclerView, layout, new ArrayList<>(), manager, listener, null, null, -1, 1);
    }
    public static <T> BaseQuickAdapter<T, BaseViewHolder> initRecyclerView(RecyclerView recyclerView, int layout, int height, RecyclerView.LayoutManager manager, AdapterInitListener<T> listener) {
        return initRecyclerView(recyclerView, layout, new ArrayList<>(), manager, listener, null, null, -1, height);
    }
    public static <T> BaseQuickAdapter<T, BaseViewHolder> initRecyclerView(RecyclerView recyclerView, int layout, RecyclerView.LayoutManager manager,int color, AdapterInitListener<T> listener) {
        return initRecyclerView(recyclerView, layout, new ArrayList<>(), manager, listener, null, null, color, 1);
    }
    public static <T> BaseQuickAdapter<T, BaseViewHolder> initRecyclerView(RecyclerView recyclerView, int layout, List<T> list, RecyclerView.LayoutManager manager, AdapterInitListener<T> listener) {
        return initRecyclerView(recyclerView, layout, list, manager, listener, null, null, -1, 1);
    }
    public static <T> BaseQuickAdapter<T, BaseViewHolder>   initRecyclerView(RecyclerView recyclerView, int layout, List<T> list, RecyclerView.LayoutManager manager,
                                                                           AdapterInitListener<T> listener, OnItemClickListener onItemClickListener) {
        return initRecyclerView(recyclerView, layout, list, manager, listener, onItemClickListener, null, -1, 1);
    }
    public static <T> BaseQuickAdapter<T, BaseViewHolder> initRecyclerView(RecyclerView recyclerView, int layout, RecyclerView.LayoutManager manager,
                                                                           int decorcolor, int decorationheight, AdapterInitListener<T> listener) {
        return initRecyclerView(recyclerView, layout, new ArrayList<>(), manager, listener, null, null, decorcolor, decorationheight);
    }
    public static <T> BaseQuickAdapter<T, BaseViewHolder> initRecyclerView(RecyclerView recyclerView, int layout, RecyclerView.LayoutManager manager, AdapterInitListener<T> listener,
                                                                           RecyclerView.ItemDecoration decoration) {
        return initRecyclerView(recyclerView, layout, new ArrayList<>(), manager, listener, null, decoration, -1, 1);
    }
    public static <T> BaseQuickAdapter<T, BaseViewHolder> initRecyclerView(RecyclerView recyclerView, int layout, RecyclerView.LayoutManager manager, AdapterInitListener<T> listener,
                                                                           OnItemClickListener onItemClickListener) {
        return initRecyclerView(recyclerView, layout, new ArrayList<>(), manager, listener, onItemClickListener, null, -1, 1);
    }

    /**
     * 初始化adapter
     *
     * @param recyclerView        view
     * @param layout              item布局
     * @param list                数据源
     * @param manager             adapter管理器
     * @param listener            adapter加载回调，处理item
     * @param onItemClickListener
     * @param decoration          间距线
     * @param decorcolor          间距线颜色
     * @param decorationheight    间距线高度
     * @param <T>                 item数据类型
     * @return 实例化适配器
     */
    public static <T> BaseQuickAdapter<T, BaseViewHolder> initRecyclerView(RecyclerView recyclerView, int layout, List<T> list, RecyclerView.LayoutManager manager, AdapterInitListener<T> listener,
                                                                           OnItemClickListener onItemClickListener, RecyclerView.ItemDecoration decoration, int decorcolor, int decorationheight) {
        BaseQuickAdapter<T, BaseViewHolder> adapter = new BaseQuickAdapter<T, BaseViewHolder>(layout, list) {
            @Override
            protected void convert(BaseViewHolder helper, T item) {
                if (null != listener) {
                    listener.convert(helper, item);
                }
            }
        };
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(null != decoration ? decoration : new UniversalItemDecoration() {
            @Override
            public Decoration getItemOffsets(int position) {
                ColorDecoration decoration1 = new ColorDecoration(-1 == decorcolor ? R.color.colorGrayF2 : decorcolor);
                decoration1.bottom = DensityUtil.dip2px(decorationheight);
                return decoration1;
            }
        });
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            if (null != onItemClickListener) {
                onItemClickListener.onItemClick(adapter1, view, position);
            }
        });
        return adapter;
    }

    /**
     * item 构建回调
     *
     * @param <T> item数据类型
     */
    public interface AdapterInitListener<T> {
        void convert(BaseViewHolder helper, T item);
    }
}
